package com.polaris.live.ui.me

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.CollectionUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SpanUtils
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.polaris.live.R.*
import com.polaris.live.adapter.chatPrice.ChatLevelAdapter
import com.polaris.live.adapter.chatPrice.TodayCallAdapter
import com.polaris.live.common.ext.formatNumberWithK
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.DateUtils
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.ActivityMyAnchorLevelBinding
import com.polaris.live.dialog.CoinBeansInstructionDialog
import com.polaris.live.dialog.TargetAnchorDialog
import com.polaris.live.resp.back_resp.CallPriceResp
import com.polaris.live.translation.R
import com.polaris.live.viewmodel.me.MeViewModel

/**
 * 我的主播等级
 *
 * @author Created by lucas on 2023/11/08 15:31
 * @since 1.0.0
 */
class MyAnchorLevelActivity : BaseVbActivity<MeViewModel, ActivityMyAnchorLevelBinding>() {

    private val mAdapter: ChatLevelAdapter by lazy {
        ChatLevelAdapter()
    }

    private val mTodayAdapter: TodayCallAdapter by lazy {
        TodayCallAdapter()
    }

    private var mCallPriceResp: CallPriceResp? = null

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setTitleBar(mBinding.bvTitle)
        setStatusBarDarkFont(false)
        setNavigationBarDarkFont(false)
        attachImmersionBar {
            transparentNavigationBar()
        }
    }

    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        mViewModel.getCallPrice()
        mBinding.recyclerView.adapter = mAdapter
        mBinding.todayRecycler.adapter = mTodayAdapter
        initObserver()
        initListener()
        showLoading(waitTime = 0)
    }

    private fun initListener() {
        mBinding.ivCoinBeans.setOnSingleClickListener {
            mCallPriceResp?.let {
                if (!CollectionUtils.isEmpty(it.beansInstruction)) {
                    XPopup.Builder(this)
                        .isDestroyOnDismiss(true)
                        .asCustom(CoinBeansInstructionDialog(this, it.beansInstruction))
                        .show()
                }
            }
        }
        mBinding.ivTargetAnchor.setOnSingleClickListener {
            mCallPriceResp?.let {
                if (!CollectionUtils.isEmpty(it.ratingInstruction)) {
                    XPopup.Builder(this)
                        .isDestroyOnDismiss(true)
                        .asCustom(TargetAnchorDialog(this, it.ratingInstruction))
                        .show()
                }
            }
        }
    }

    private fun initObserver() {
        mViewModel.mCallPriceLiveData.observe(this) { resultState ->
            dismissLoading()
            parseState(resultState, {
                mCallPriceResp = it
                mBinding.scrollView.visibility = View.VISIBLE
                it.let {
                    setBeans(it)
                    mBinding.tvLevel.setLevel(it.level)
                    mAdapter.setIsDetail(true)
                    mAdapter.setList(it.ruleList)
                }
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }
    }

    private fun setBeans(callPriceResp: CallPriceResp) {
        val span = SpanUtils.with(mBinding.tvAllBeans)
            .append("(${callPriceResp.beans.formatNumberWithK()}/")
            .setForegroundColor(ColorUtils.getColor(color.color_000000_50))
            .append(callPriceResp.beansTotal.formatNumberWithK())
            .setForegroundColor(ColorUtils.getColor(color.color_9D6AFF))
            .append(")")
            .setForegroundColor(ColorUtils.getColor(color.color_000000_50))
            .create()
        mBinding.tvAllBeans.text = span

        //当小于等于0的时候代表已经是最大了    直接隐藏然后展示对号即可
        val remainBeans = callPriceResp.beansTotal - callPriceResp.beans
        if (remainBeans <= 0) {
            mBinding.beansRemain.visibility = View.GONE
        } else {
            mBinding.beansRemain.text = remainBeans.formatNumberWithK()
            mBinding.ivBeans.visibility = View.GONE
        }
        //进度条设置 如果等于0则进度条和上方的数字都不展示
        if (callPriceResp.beansTotal == 0L) {
            mBinding.llBeans.visibility = View.GONE
            mBinding.progressBar.visibility = View.GONE
        } else {
            mBinding.progressBar.progress =
                (callPriceResp.beans * 100 / callPriceResp.beansTotal).toInt()
        }

        val span2 = SpanUtils.with(mBinding.tvAllCome)
            .append("(${callPriceResp.payBeans.formatNumberWithK()}/")
            .setForegroundColor(ColorUtils.getColor(color.color_000000_50))
            .append(callPriceResp.payBeansTotal.formatNumberWithK())
            .setForegroundColor(ColorUtils.getColor(color.color_9D6AFF))
            .append(")")
            .setForegroundColor(ColorUtils.getColor(color.color_000000_50))
            .create()
        mBinding.tvAllCome.text = span2
        //当小于等于0的时候代表已经是最大了    直接隐藏然后展示对号即可
        val remainPayBeans = callPriceResp.payBeansTotal - callPriceResp.payBeans
        if (remainPayBeans <= 0) {
            mBinding.payBeansRemain.visibility = View.GONE
        } else {
            mBinding.payBeansRemain.text = remainPayBeans.formatNumberWithK()
            mBinding.ivPayBeans.visibility = View.GONE
        }
        //进度条设置 如果等于0则进度条和上方的数字都不展示
        if (callPriceResp.payBeansTotal == 0L) {
            mBinding.llPayBeans.visibility = View.GONE
            mBinding.payProgressBar.visibility = View.GONE
        } else {
            mBinding.payProgressBar.progress =
                (callPriceResp.payBeans * 100 / callPriceResp.payBeansTotal).toInt()
        }

        mBinding.tvTodayCallNum.text =
            StringUtils.getString(R.string.today_call_num, callPriceResp.indicator.todayCount)

        if (CollectionUtils.isEmpty(callPriceResp.indicator.indicators)) {
            mBinding.llTodayCall.visibility = View.GONE
        } else {
            mTodayAdapter.setList(callPriceResp.indicator.indicators)
        }
        val liveTime = DateUtils.convertSecondsToMinutes(callPriceResp.liveTime.liveTime)
        val waitTime = DateUtils.convertSecondsToMinutes(callPriceResp.liveTime.waitTime)
        val partyTime = DateUtils.convertSecondsToMinutes(callPriceResp.liveTime.partyTime)
        mBinding.liveTime.text = liveTime.toString()
        mBinding.matchLiveTime.text = waitTime.toString()
        mBinding.partyTime.text = partyTime.toString()

        if (callPriceResp.maxLevel == true) {
            mBinding.tvTitle.visibility = View.GONE
            mBinding.llBeans.visibility = View.GONE
            mBinding.llPayBeans.visibility = View.GONE
            mBinding.payProgressBar.visibility = View.GONE
            mBinding.progressBar.visibility = View.GONE
        }
    }
}